let favorite;//存储收藏对象
document.getElementById("favorite_name").addEventListener("blur", function () {
    //todo
    var reg=/^[.]{1,6}$/;
    if (!reg.test(this.value)){
        //alert("链接名称长度有错误");
    }

})
//1. 当页面加载完成后，发送ajax请求
window.onload = function () {
    //获取收藏的id
    //console.log(location.href);
    const params = new URLSearchParams(window.location.search);
    const favoriteId = params.get('favoriteId');
    //发送ajax请求获取该收藏的详细信息
    axios({
        method:"get",
        url:"getMyFavoriteById",
        params:{"favoriteId":favoriteId}
    }).then(function (resp) {
        //console.log(resp.data);
        favorite = resp.data;
        // 获取表单数据
        document.getElementById("favorite_name").value=favorite.favoriteName;
        // 获取表单数据
        document.getElementById("link_url").value=favorite.linkUrl;
        // 获取表单数据
        document.getElementById("login_name").value=favorite.loginName;
        // 获取表单数据
        document.getElementById("password").value=favorite.password;
        //发送ajax请求获取我的收藏分类并恢复该收藏的分类
        axios({
            method:"get",
            url:"getMyFavoriteCategorys"
        }).then(function (resp) {
            let list = resp.data;
            let favoriteCategorys="收藏分类：";
            for (let i = 0; i < list.length ; i++) {
                let obj = list[i];
                //<input type="radio" name="departmentId" value="${department.id}" >${department.name}
                if(obj.id == favorite.categoryId){
                    favoriteCategorys += "<input type=\"radio\" name=\"favorite_category_id\" value="+
                        obj.id +" checked=\"checked\">"+obj.categoryName;
                }else {
                    favoriteCategorys += "<input type=\"radio\" name=\"favorite_category_id\" value="+
                        obj.id +" >"+obj.categoryName;
                }

            }
            // 设置表格数据
            document.getElementById("my_link_category").innerHTML = favoriteCategorys;
        })
    });

}
//1. 给按钮绑定单击事件
document.getElementById("btn").onclick = function () {
    // 获取表单数据
    favorite.favoriteName = document.getElementById("favorite_name").value;
    // 获取表单数据
    favorite.linkUrl= document.getElementById("link_url").value;
    // 获取表单数据
    favorite.loginName= document.getElementById("login_name").value;
    // 获取表单数据
    favorite.password= document.getElementById("password").value;

    let favoriteCategorys = document.getElementsByName("favorite_category_id");
    for (let i = 0; i < favoriteCategorys.length; i++) {
        if(favoriteCategorys[i].checked){
            //获取选中的链接分类的id
            favorite.categoryId = favoriteCategorys[i].value ;
        }
    }
    console.log(favorite);
    //2. 发送ajax请求
    axios({
        method:"post",
        url:"editFavorite",
        data:favorite
    }).then(function (resp) {
        // 判断响应数据是否为 success
        if(resp.data == "success"){
            location.href = "my_favorite_list.html";
        }else {
            alert("保存失败！")
        }
    })
}